#!/bin/bash


# ===================
# 监控磁盘使用情况
# cron 
# * * * * * sh /u01/monitor_disk.sh
# ===================

# 配置参数
DATA_DIR="/u01/pgsql14/data"          # 监控的数据目录
LOG_FILE="/var/log/disk_usage.log"  # 日志文件路径
MAX_LOG_DAYS=30           # 日志保留天数

# 创建日志目录(如果不存在)
mkdir -p $(dirname $LOG_FILE)

# 获取当前时间戳
timestamp=$(date "+%Y-%m-%d %H:%M:%S")

# 获取磁盘使用信息
disk_info=$(df -h $DATA_DIR | tail -1)
filesystem=$(echo $disk_info | awk '{print $1}')
total=$(echo $disk_info | awk '{print $2}')
used=$(echo $disk_info | awk '{print $3}')
available=$(echo $disk_info | awk '{print $4}')
use_percent=$(echo $disk_info | awk '{print $5}')
mount_point=$(echo $disk_info | awk '{print $6}')

# 写入日志文件
echo "$timestamp | $filesystem | Total: $total | Used: $used | Available: $available | Usage: $use_percent | Mount: $mount_point" >> $LOG_FILE
du -sh /u01/* 2>/dev/null | sort -hr >> $LOG_FILE